//
//  AuctionScoreConverListVC.m
//  MyNewProject
//
//  Created by 王春辉 on 2020/2/1.
//  Copyright © 2020年 sun. All rights reserved.
//
#define kTabDefautHeight 47.0
#import "AuctionScoreConverListVC.h"
#import "ProductDetailVC.h"
#import "OrderCell.h"
#import "WJBaseModel.h"
#import <WebKit/WebKit.h>

@interface AuctionScoreConverListVC ()<UITableViewDelegate,UITableViewDataSource> {
    NSInteger _page;
}
@property (nonatomic, strong) UITableView * myTalbleView;
@property (nonatomic, strong) UIView * notDataView;
@property (nonatomic, strong) NSArray *dataArry;

@end

@implementation AuctionScoreConverListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.gk_navTitle = @"兑换记录";
    self.view.backgroundColor = SKRGBColor(237, 237, 237);
    self.dataArry = [[NSMutableArray alloc] initWithCapacity:0];
    [self.view addSubview:self.myTalbleView];
    [self.notDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.myTalbleView);
    }];
    [self.myTalbleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_offset(SKTopBar_Height);
        make.bottom.mas_offset(-SKBottomSafe_Height);
    }];
    self.myTalbleView.mj_header = [MJRefreshStateHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
    self.myTalbleView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreData)];
    [self getData];
}


#pragma mark -- mark 懒加载
- (UIView *)notDataView {
    if (!_notDataView) {
        self.notDataView = [[UIView alloc] init];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"orderMissing"]];
        imageView.frame = CGRectMake((SCREEN_WIDTH - 93)/2,SKTopBar_Height + 71,93,85.5);
        [_notDataView addSubview:imageView];
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(0,imageView.bottom + 18,SCREEN_WIDTH,20);
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentCenter;
        label.font = SKFONT_Semibold(18);
        label.textColor = SKColorWithHex(0x888888);
        label.text = @"暂无数据";
        [_notDataView addSubview:label];
        
        NSString * totalAmtStr = @"快去竞拍吧";
        UIFont * font = SKFONT_BOLD_SIZE(18);
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake((SCREEN_WIDTH -167)/2, label.bottom + 48, 167 , 41);
        button.imageView.contentMode = UIViewContentModeScaleAspectFit;
        button.backgroundColor = SKColorWithHex(0xE33030);
        [button setTitle:totalAmtStr forState:UIControlStateNormal];
        button.titleLabel.font = font ;
        [button setTitleColor:SKColorWithHex(0xFFFFFF) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(goJingPai:) forControlEvents:UIControlEventTouchUpInside];
        [_notDataView addSubview:button];
        [self.view addSubview:_notDataView];
    }
    return _notDataView;
}
-(UITableView*)myTalbleView
{
    if (_myTalbleView ==nil) {
        _myTalbleView =[[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTalbleView.dataSource = self;
        _myTalbleView.backgroundColor = SKRGBColor(237, 237, 237);
        _myTalbleView.delegate = self;
        _myTalbleView.estimatedRowHeight = 0;
        _myTalbleView.estimatedSectionFooterHeight = 0;
        _myTalbleView.estimatedSectionHeaderHeight = 0;
        _myTalbleView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_myTalbleView registerClass:[OrderCell class] forCellReuseIdentifier:@"identifier"];
        if (@available(iOS 11.0, *)) {
            _myTalbleView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
    }
    return _myTalbleView;
}
- (NSArray *)dataArry {
    if (!_dataArry) {
        _dataArry = [NSArray array];
    }
    self.notDataView.hidden = _dataArry.count > 0;
    return _dataArry;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier" forIndexPath:indexPath];
    OrderData *model = self.dataArry[indexPath.row];
    cell.cellModel = model;
    cell.myBlock = ^(OrderData *  model, NSInteger tag) {
        // 1 补齐尾款 2 复制单号 3 联系客服
        switch (tag) {
            case 1:
                
                break;
            case 2: {
                UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
                pasteboard.string = SafeString(model.num);
            }
                break;
            case 3: {
                WKWebView*callWebview =[[WKWebView alloc] init];
                NSURL *telURL =[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",model.num]];// 貌似tel:// 或者 tel: 都行
                [callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
                //记得添加到view上
                [self.view addSubview:callWebview];
            }
                break;
            default:
                break;
        }
    };
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80 +30 +10;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderData *data = self.dataArry[indexPath.row];
    ProductDetailVC *vc = [[ProductDetailVC alloc] init];
    vc.ptId = data.typeId;
    vc.type = 1;
    vc.showType = ProductDetailTypeLog;
    [self.navigationController pushViewController:vc animated:true];
}

#pragma mark -- mark 网络请求
- (void)getData {
    _page = 1;
    SKWeakSelf;
    [self getDataWithPage:_page Compelete:^(id response) {
        OrderBaseClass * baseClass = [OrderBaseClass modelObjectWithDictionary:response];
        NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:baseClass.data.count];
        for (OrderData *model in baseClass.data) {
            if ([model.type intValue] == 3) {
                [mArr addObject:model];
            }
        }
        weakSelf.dataArry = mArr;
        [weakSelf.myTalbleView reloadData];
    }];
}
- (void)getMoreData {
    if (_page == 0) {
        [self endRefreshing];
        return; }
    _page += 1;
    SKWeakSelf;
    [self getDataWithPage:_page Compelete:^(id response) {
        OrderBaseClass * baseClass = [OrderBaseClass modelObjectWithDictionary:response];
        NSMutableArray *mArr = [NSMutableArray arrayWithArray:weakSelf.dataArry];
        for (OrderData *model in baseClass.data) {
            if ([model.type intValue] == 3) {
                [mArr addObject:model];
            }
        }
        weakSelf.dataArry = mArr;
        [weakSelf.myTalbleView reloadData];
    }];
}

- (void)getDataWithPage:(NSInteger)page Compelete:(void(^)(id response))complete {
    NSDictionary *param = @{@"uid":self.uid,
                            @"loginKey":self.loginKey,
                            @"type":@(3), // 积分兑换
                            @"page":@(page)
                            };
    SKWeakSelf;
    [YQNetworking postWithUrl:@"My/getOrderList" params:param reloginRefreshBlock:^(id response) {
        
    } successBlock:^(id response) {
        WJBaseModel *model = [WJBaseModel modelWithDictionary:response];
        if ([model isSuccess]) {
            if (complete) {
                complete(response);
            }
        }else {
            [weakSelf showMessage:model.msg];
        }
        [weakSelf endRefreshing];
    } failBlock:^(NSError *error) {
        [weakSelf endRefreshing];
        [weakSelf debugShowMessage:error.localizedDescription];
    }];
}

- (void)endRefreshing {
    if ([self.myTalbleView.mj_header isRefreshing]) {
        [self.myTalbleView.mj_header endRefreshing];
    }
    if ([self.myTalbleView.mj_footer isRefreshing]) {
        [self.myTalbleView.mj_footer endRefreshing];
    }
}
#pragma mark -- mark EVENT
- (void)goJingPai:(UIButton *)sender {
    self.tabBarController.selectedIndex = 0;
}
@end
